#include "opencv_edge_det.h"

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

void doOpencvEdgeDet1()
{
	Mat img = imread("test.jpg");
	imshow("src", img);
	Mat DstPic, edge, grayImage;

	//创建与src同类型和同大小的矩阵
	DstPic.create(img.size(), img.type());

	//将原始图转化为灰度图
	cvtColor(img, grayImage, COLOR_BGR2GRAY);
	imshow("gray", grayImage);

	//先使用3*3内核来降噪
	blur(grayImage, edge, Size(3, 3));
	imshow("blur", edge);

	//运行canny算子
	Canny(edge, edge, 3, 9, 3);
	imshow("Canny", edge);

	waitKey(0);

}


void doOpencvEdgeDet2()
{
	Mat img = imread("test.jpg");
	imshow("src", img);

	Mat grayImage;
	cvtColor(img, grayImage, COLOR_BGR2GRAY);
	imshow("gray", grayImage);

	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y, dst;

	//求x方向梯度
	Sobel(grayImage, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
	convertScaleAbs(grad_x, abs_grad_x);
	imshow("soble_x", abs_grad_x);

	//求y方向梯度
	Sobel(grayImage, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
	convertScaleAbs(grad_y, abs_grad_y);
	imshow("soble_y", abs_grad_y);

	//合并梯度
	addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dst);
	imshow("soble_addWeighted", dst);


	waitKey(0);

}


void doOpencvEdgeDet3()
{
	Mat img = imread("test.jpg");
	imshow("src", img);
	Mat gray, dst, abs_dst;
	//高斯滤波消除噪声
	GaussianBlur(img, img, Size(3, 3), 0, 0, BORDER_DEFAULT);
	imshow("GaussianBlur", img);
	//转换为灰度图
	cvtColor(img, gray, COLOR_RGB2GRAY);
	imshow("gray", gray);
	//使用Laplace函数
	//第三个参数：目标图像深度；第四个参数：滤波器孔径尺寸；第五个参数：比例因子；第六个参数：表示结果存入目标图
	Laplacian(gray, dst, CV_16S, 3, 1, 0, BORDER_DEFAULT);
	//计算绝对值，并将结果转为8位
	convertScaleAbs(dst, abs_dst);

	imshow("Laplacian", abs_dst);

	waitKey(0);

}

void doOpencvEdgeDet4()
{
	Mat img = imread("test.jpg");
	imshow("src", img);
	Mat lab, blur, dst;
	//双边滤波
	bilateralFilter(img, blur, 9, 75, 75);
	imshow("bilateralFilter", img);
	//转换为Lab图
	cvtColor(img, lab, COLOR_BGR2Lab);
	imshow("lab", lab);

	vector<Mat> lab_channels;
	split(lab, lab_channels);
	Mat l_channel = lab_channels[0];  // 亮度通道
	imshow("l_channel", l_channel);

	cv::Ptr<CLAHE> clahe = createCLAHE(3.0);

	Mat equlized;
	clahe->apply(l_channel, equlized);
	imshow("equlized", equlized);


	//运行canny算子
	Canny(equlized, dst, 30, 90);
	imshow("dst", dst);

	waitKey(0);
}

